package com.twohand.admin.Controller;

import com.twohand.admin.Annotation.Permission;
import com.twohand.admin.Dto.*;
import com.twohand.admin.Entity.Shop;
import com.twohand.admin.Service.ShopService;
import com.twohand.admin.Util.PageResponse;
import com.twohand.admin.Vo.GenUrlVo;
import com.twohand.admin.Vo.Order.OrderStatisVo;
import com.twohand.admin.Vo.SelectOptions;
import com.twohand.utils.Response.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    ShopService shopService;
    @GetMapping("/options")
    List<SelectOptions> getOptions(){
        return shopService.getOptions();
    }
    @GetMapping("/shopNameOptions")
    List<SelectOptions> shopNameOptions(){
        return shopService.getShopNameOption();
    }
    @Permission(value = "shop:list")
    @GetMapping
    PageResponse<Shop> shopList(ShopListQueryDto shopListQueryDto){
        return shopService.shopList(shopListQueryDto);
    }
    @Permission(value = "shop:add")
    @PostMapping
    Boolean addShop(@RequestBody Shop shop){
        return shopService.addShop(shop);
    }
    @Permission(value = "shop:edit")
    @PutMapping
    Boolean updateShop(@RequestBody Shop shop){
        return shopService.changShop(shop);
    }
    @Permission(value = "shop:delete")
    @DeleteMapping("/{id}")
    Boolean delShop(@PathVariable Integer id){
        return shopService.delShop(id);
    }

    @PostMapping("/shopCheck")
    Object checkShopExist(@RequestBody List<ShopInfoDto> shopInfos){
        return shopService.checkAndAddShop(shopInfos);
    }
    @Permission(value = "shop:generate")
    @GetMapping("/getUrl")
    GenUrlVo getUrl(GenUrlDto genUrlDto) throws ParseException {


        Shop shop = shopService.selectShopById(genUrlDto.getId());
        if(shop == null){
            throw new BaseException(500,"没有找到店铺");
        }
        if(StringUtils.isNoneEmpty(shop.getRelevancy())){
            return shopService.getUrlNew(genUrlDto);
        }else{
            return shopService.getUrl(genUrlDto);
        }

    }

    @GetMapping("/orderStatis")
    OrderStatisVo OrderStatis(ShopListQueryDto shopListQueryDto){
        return shopService.OrderStatis(shopListQueryDto);
    }
    @GetMapping("/groupDate")
    List<String> groupDate(ShopListQueryDto shopListQueryDto){
        return shopService.groupByActiveDate(shopListQueryDto);
    }

    @Permission(value = "shop:list")
    @PostMapping("priority/{id}")
    Boolean priorityShop(@PathVariable Integer id){
        return shopService.priorityShop(id);
    }


    @Permission("shop:list")
    @PostMapping("/batchChangeRelevancy")
    Boolean batchChangeRelevancy(@RequestBody BatchChangeMoneyDto batchChangeMoneyDto){
        return shopService.batchChangeRelevancy(batchChangeMoneyDto);
    }

}
